/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mayo_5.Ej_20_05_13_01;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.File;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author vesprada
 */
public class Matriz extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame1
     */
    File archivo1;
    File archivo2;
    File archivo;
    DefaultTableModel modelo1;
    DefaultTableModel modelo2;
    DefaultTableModel modelo3;
    int[][] matriz1;
    int[][] matriz2;
    int[][] matrizr;
    Scanner lector1;
    Scanner lector2;
    JFileChooser chooser;

    public Matriz ()
    {
        initComponents();
        archivo = new File("/home/vesprada/NetBeansProjects/TercerTrimestre");
        chooser = new JFileChooser(archivo);
////        modelo1 = new DefaultTableModel();
////        modelo2 = new DefaultTableModel();
////        modelo3 = new DefaultTableModel();
        JScrollBar scrollHorizontal = new JScrollBar(JScrollBar.HORIZONTAL);
//        ImageIcon newIcon = new javax.swing.ImageIcon(getClass().getResource("/imagenes/carpetas.png"));
        if ( System.getProperty("os.name").toLowerCase().contains("windows") )
          {
            Image img = new javax.swing.ImageIcon(getClass().getResource("/imagenes/carpetas.png")).getImage();
            Image newimg = img.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
            ImageIcon newIcon = new ImageIcon(newimg);
            jButton2.setIcon(newIcon);
            jButton1.setIcon(newIcon);
          }
        else
          {
            Image img = new javax.swing.ImageIcon(getClass().getResource("imagenes/carpetas.png")).getImage();
            Image newimg = img.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
            ImageIcon newIcon = new ImageIcon(newimg);
            jButton2.setIcon(newIcon);
            jButton1.setIcon(newIcon);
          }



        this.pack();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        rbg_oper = new javax.swing.ButtonGroup();
        pn_datos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tb_1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tb_2 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txt_archivo2 = new javax.swing.JTextField();
        txt_archivo1 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        rb_suma = new javax.swing.JRadioButton();
        rb_resta = new javax.swing.JRadioButton();
        but_calcular = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tb_3 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();

        setTitle("Lector Matrices");
        setResizable(false);

        pn_datos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        tb_1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {

            }
        ));
        jScrollPane1.setViewportView(tb_1);

        jLabel1.setText("Matriz 1");

        tb_2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {

            }
        ));
        jScrollPane2.setViewportView(tb_2);

        jLabel2.setText("Matriz 2");

        jButton1.setFont(new java.awt.Font("Dialog", 1, 10)); // NOI18N
        jButton1.setText("...");
        jButton1.setMargin(new java.awt.Insets(2, 4, 2, 4));
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Dialog", 1, 10)); // NOI18N
        jButton2.setText("...");
        jButton2.setMargin(new java.awt.Insets(2, 4, 2, 4));
        jButton2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton2ActionPerformed(evt);
            }
        });

        txt_archivo2.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
        txt_archivo2.setToolTipText("Ruta archivo 2");
        txt_archivo2.setEnabled(false);

        txt_archivo1.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
        txt_archivo1.setToolTipText("Ruta archivo 1");
        txt_archivo1.setEnabled(false);

        javax.swing.GroupLayout pn_datosLayout = new javax.swing.GroupLayout(pn_datos);
        pn_datos.setLayout(pn_datosLayout);
        pn_datosLayout.setHorizontalGroup(
            pn_datosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_datosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_datosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(pn_datosLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_archivo2, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addGroup(pn_datosLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_archivo1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        pn_datosLayout.setVerticalGroup(
            pn_datosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_datosLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(pn_datosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton2)
                    .addComponent(txt_archivo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pn_datosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jButton1)
                    .addComponent(txt_archivo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Operaciones"));

        rbg_oper.add(rb_suma);
        rb_suma.setText("Suma");

        rbg_oper.add(rb_resta);
        rb_resta.setText("Resta");

        but_calcular.setText("Calcular");
        but_calcular.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                but_calcularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(rb_suma)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(rb_resta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(but_calcular)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rb_suma)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rb_resta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(but_calcular)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado"));

        tb_3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {

            }
        ));
        jScrollPane3.setViewportView(tb_3);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton3.setText("Salir");
        jButton3.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pn_datos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pn_datos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3)))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
//        chooser.showDialog(this, "Archivo 1");
        if ( chooser.showDialog(this, "Archivo 1") == JFileChooser.CANCEL_OPTION )
          {
            chooser.setSelectedFile(null);
          }
        if ( chooser.getSelectedFile() == null )
          {
            JOptionPane.showMessageDialog(this, "Elije un archivo");
          }
        else
          {
            archivo1 = new File(chooser.getSelectedFile().toString());
            try
              {
                lector1 = new Scanner(archivo1);
                lector1.useDelimiter(",|\n");
              } catch (Exception e)
              {
                System.out.println("error1");
              }
            txt_archivo1.setText(archivo1.toString());
            matriz1 = new int[Character.getNumericValue(lector1.nextLine().charAt(0))][Character.getNumericValue(lector1.nextLine().charAt(0))];
            modelo1 = new DefaultTableModel(matriz1.length, matriz1[0].length);
            //modelo
            if ( modelo2 == null && modelo3 == null )
              {
                modelo2 = new DefaultTableModel(matriz1.length, matriz1[0].length);
                tb_2.setModel(modelo2);
                modelo3 = new DefaultTableModel(matriz1.length, matriz1[0].length);
                tb_3.setModel(modelo3);
              }
            // pos 2
            try
              {
              } catch (Exception e)
              {
              }
            tb_1.setModel(modelo1);
            // leer
            while ( lector1.hasNext() )
              {
                for ( int f = 0; f < matriz1.length; f++ )
                  {
                    for ( int c = 0; c < matriz1[0].length; c++ )
                      {
                        matriz1[f][c] = Integer.parseInt(lector1.next());
                        tb_1.setValueAt(matriz1[f][c], f, c);
                      }
                  }
              }
            lector1.close();
          }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if ( chooser.showDialog(this, "Archivo 2") == JFileChooser.CANCEL_OPTION )
          {
            chooser.setSelectedFile(null);
          }
        if ( chooser.getSelectedFile() == null )
          {
            JOptionPane.showMessageDialog(this, "Elije un archivo");
          }
        else
          {
            archivo2 = new File(chooser.getSelectedFile().toString());
            try
              {
                lector2 = new Scanner(archivo2);
                lector2.useDelimiter(",|\n");
              } catch (Exception e)
              {
                System.out.println("error2");
              }
            txt_archivo2.setText(archivo2.toString());
            matriz2 = new int[Character.getNumericValue(lector2.nextLine().charAt(0))][Character.getNumericValue(lector2.nextLine().charAt(0))];
            modelo2 = new DefaultTableModel(matriz2.length, matriz2[0].length);
            //modelos
            if ( modelo1 == null && modelo3 == null )
              {
                modelo1 = new DefaultTableModel(matriz2.length, matriz2[0].length);
                tb_1.setModel(modelo1);
                modelo3 = new DefaultTableModel(matriz2.length, matriz2[0].length);
                tb_3.setModel(modelo3);
              }
            // pos 2
            try
              {
              } catch (Exception e)
              {
              }
            tb_2.setModel(modelo2);
            // leer
            while ( lector2.hasNext() )
              {
                for ( int f = 0; f < matriz2.length; f++ )
                  {
                    for ( int c = 0; c < matriz2[0].length; c++ )
                      {
                        matriz2[f][c] = Integer.parseInt(lector2.next());
                        tb_2.setValueAt(matriz2[f][c], f, c);
                      }
                  }
              }
            lector2.close();
          }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
//        System.exit(0);
        this.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void but_calcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_calcularActionPerformed
        // TODO add your handling code here:
        matrizr = new int[matriz2.length][matriz2[0].length];
        modelo3 = new DefaultTableModel(matrizr.length, matrizr[0].length);
        tb_3.setModel(modelo3);
        if ( rb_suma.isSelected() )
          {
            for ( int f = 0; f < matriz2.length; f++ )
              {
                for ( int c = 0; c < matriz2[0].length; c++ )
                  {
                    matrizr[f][c] = matriz1[f][c] + matriz2[f][c];
                    tb_3.setValueAt(matrizr[f][c], f, c);
                  }
              }
          }
        else if ( rb_resta.isSelected() )
          {
            for ( int f = 0; f < matriz2.length; f++ )
              {
                for ( int c = 0; c < matriz2[0].length; c++ )
                  {
                    matrizr[f][c] = matriz1[f][c] - matriz2[f][c];
                    tb_3.setValueAt(matrizr[f][c], f, c);
                  }
              }
          }
        else
          {
            JOptionPane.showMessageDialog(this, "Elije una opción antes de calcular");
          }
    }//GEN-LAST:event_but_calcularActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main (String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel.
         * For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try
          {
            for ( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels() )
              {
                if ( "Nimbus".equals(info.getName()) )
                  {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                  }
              }
          } catch (ClassNotFoundException ex)
          {
            java.util.logging.Logger.getLogger(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex)
          {
            java.util.logging.Logger.getLogger(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex)
          {
            java.util.logging.Logger.getLogger(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex)
          {
            java.util.logging.Logger.getLogger(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run ()
            {
                new Matriz().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton but_calcular;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel pn_datos;
    private javax.swing.JRadioButton rb_resta;
    private javax.swing.JRadioButton rb_suma;
    private javax.swing.ButtonGroup rbg_oper;
    private javax.swing.JTable tb_1;
    private javax.swing.JTable tb_2;
    private javax.swing.JTable tb_3;
    private javax.swing.JTextField txt_archivo1;
    private javax.swing.JTextField txt_archivo2;
    // End of variables declaration//GEN-END:variables
}
